package ir.values.users.instructions.alu;

import ir.types.datatypes.DataType;
import ir.types.datatypes.IntegerType;
import ir.values.Value;
import ir.values.users.constants.IntegerConst;
import ir.values.users.instructions.InstOperator;

public class IcmpInst extends AluInst {
    private final IcmpCondition condition;

    public IcmpInst(IcmpCondition condition, Value operand1, Value operand2) {
        super(IntegerType.I1, operand1, operand2);
        this.condition = condition;
    }

    public IcmpInst(Value operand1) {
        this(IcmpCondition.ne, operand1, new IntegerConst(0));
    }

    public enum IcmpCondition {
        eq, ne, sgt, sge, slt, sle
    }

    @Override
    public InstOperator getInstOperator() {
        return InstOperator.icmp;
    }

    @Override
    public String parseLLVM() {
        StringBuilder sb = new StringBuilder();
        sb
                .append(parseUsingName())
                .append(" = ")
                .append(getInstOperator())
                .append(" ")
                .append(condition.toString())
                .append(" ")
                .append(operand1.getType())
                .append(" ")
                .append(operand1.parseUsingName())
                .append(", ")
                .append(operand2.parseUsingName());
        return sb.toString();
    }
}
